package pl.mdziedzic.unittests;

import static junit.framework.Assert.*;
import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

import pl.mdziedzic.unittests.services.RequestContext;

public class AuctionTest {
	private RequestContext context;
	private User user;

	@Before
	public void setUp() {
		user = new User("iHateJava");
		context = new RequestContext(user);
	}

	@Test
	public void shouldVerifyifBuyNowAuctionIsCreatedCorrectly() {

		// given
		CreateAuctionRequest request = createBuyNowAuctionRequest();

		// when
		Auction auction = new Auction(context, request);

		// then
		assertThatBuyNowAuctionIsCorrect(request, auction);
//ff
	}
	
	@Test
	public void shouldVeryfiIfAuctionStarted () {
		// given
		CreateAuctionRequest request = createBuyNowAuctionRequest();
		Auction auction = new Auction(context, request);
		Calendar now = context.getNow();
		now.add(Calendar.DATE,1);
		now.add(Calendar.SECOND,1);
		//when
		auction.activate();
		
		//then
		assertEquals(AUCTION_STATUS.ACTIVE, auction.getStatus());
		assertEquals(now, auction.getActivateDate());
		
	}

	private void assertThatBuyNowAuctionIsCorrect(CreateAuctionRequest request,
			Auction auction) {
		assertFalse(auction.hasBids());
		assertEquals(request.getName(), auction.getName());
		assertEquals(request.getStartTime(), auction.getStartTime());
		assertEquals(request.getEndTime(), auction.getEndTime());
		assertEquals(AUCTION_TYPE.BUY_NOW, auction.getType());
		assertEquals(AUCTION_STATUS.NEW, auction.getStatus());
		assertEquals(request.getBuyNowPrice(), auction.getBuyNowPrice());
		assertEquals(user, auction.getOwner());
	}

	private CreateAuctionRequest createBuyNowAuctionRequest() {
		String auctionName = "moja aukcja";
		int price = 100;
		Calendar startTime = Calendar.getInstance();
		startTime.add(Calendar.DATE,1);
		Calendar endTime = Calendar.getInstance();
		endTime.add(Calendar.DATE,7);
		return CreateAuctionRequest.createBuyNowAuction(auctionName, startTime,
				endTime, price);
	}
}
